<template>
  <el-form ref="form" label-position="right" label-width="80px" :model="formData" :inline="inline">
    <el-form-item v-for="attr in formAttr" :key="attr.label" :label="attr.label">
      <el-input
          clearable
          v-if="attr.type==='input'"
          :placeholder="'请输入' + attr.label"
          v-model="formData[attr.name]"
      ></el-input>

      <el-switch
          v-if="attr.type === 'switch'"
          v-model="formData[attr.name]"
      ></el-switch>

      <el-date-picker
          v-if="attr.type === 'date'"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          v-model="formData[attr.name]"
      ></el-date-picker>

      <el-select
          v-if="attr.type === 'select'"
          placeholder="请选择"
          v-model="formData[attr.name]"
      >
        <el-option
            v-for="opt in attr.opts"
            :key="opt.value"
            :label="opt.label"
            :value="opt.value"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item><slot></slot></el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "CommonForm",
  // props: 父组件向子组件传值，props所在的组件为子组件
  props: {
    // 表单属性
    formAttr: Array,
    // 表单数据
    formData: Object,
    // 表单布局
    inline: Boolean
  },
  data() {
    return {}
  }
}
</script>

<style lang="less" scoped>
.el-form {
  align-items: center;
}

</style>